<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>收货地址</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@1.6/lib/index.css">
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
		<script src="https://cdn.jsdelivr.net/npm/vant@1.6/lib/vant.min.js"></script>
		<script src="html5plus://ready"></script>
		<script src="../../js/fanhui.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/tuichu.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/ajax.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			.white {
				color: white;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<header>
				<van-nav-bar @click-left="onClickLeft" @click-right="onClickRight" style="background-color: red;">
					<van-icon slot="left" name="arrow-left" color="white"></van-icon>
					<label slot="title" class="white">收货地址</label>
					<label slot="right" class="white">删除</label>
				</van-nav-bar>
			</header>
			<section>
				<van-address-list v-model="chosenAddressId" :list="list" @add="onAdd" @edit="onEdit"></van-address-list>
			</section>
		</div>
	</body>
	<script type="text/javascript">
		var newUser = localStorage.getItem("user"); //从localStorage里取数据(字符串格式)
		newUser = JSON.parse(newUser); //把字符串格式转换成Json格式
		var vm = new Vue({
			el: "#app",
			data: {
				chosenAddressId: 0, //选中Id
				list: []


			},
			created: function() {
				var _this = this;
				ajax({ //请求收货地址数据——————————————问题:进入地址需要选中默认地址吗
					url: "http://dsapi.ysd3g.com/API/MyShippingAddressList",
					dataType: "jsonp",
					data: {
						key: newUser.Key, //登陆返回的Key
					},
					success: function(res) {
						_this.list = res.Data;
						console.log(res);
					}
				})
			},
			methods: {
				onClickLeft: function() {//返回个人中心
					plus.webview.close(plus.webview.currentWebview(), "slide-out-left");
				},
				onClickRight: function() { //删除
					var _this = this;
					if (_this.chosenAddressId == 0) {
						_this.$toast("请选择要删除的地址");
					} else {
						_this.$dialog.confirm({
							title: '删除',
							message: '确认删除吗？'
						}).then(() => {//确认
							ajax({ //删除收货地址
								url: "http://dsapi.ysd3g.com/api/DeleteShippingAddress",
								dataType: "jsonp",
								data: {
									key: newUser.Key, //登陆返回的Key
									addressId: _this.chosenAddressId //要删除的地址Id
								},
								success: function(res) {
									if (res.Error) {
										_this.$toast("出现错误，请稍后再试！");
									} else {
										_this.$toast("删除成功！");
										var shouhuo = plus.webview.getWebviewById("shouhuo");
										shouhuo.reload(true);
									}
								}
							});
						}).catch(() => {//取消
						
						});
					}
				},
				onAdd: function() {
					var xinzeng=plus.webview.open("xinzengDZ.html","xinzengdz",{
						zindex:1,
						scrollIndicator:'none'
					});
					console.log("添加收货地址");
				},
				onEdit: function(msg) { //修改
					var code=localStorage.getItem("code");
					console.log(code);
					var w = plus.webview.create("bianjishouhuodizhi.html", "bianjishouhuodizhi", {}, {
						key: newUser.Key, //登陆返回的Key
						addressId:msg.id, //要编辑的地址Id
						name:msg.name,
						phone:msg.tel,
						districtCode:code,
						address:msg.address,
						isdefault:msg.IsDefault
					}); 
					w.show();
				}
			}
		})
	</script>
</html>